package com.rcp.server;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rcp.objects.Order;
import com.rcp.objects.OrderHistory;

@WebServlet("/History")
public class History extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public History() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String url = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
        String username = "cs437group01";
        String password = "VHj#dj!v";
        
        ArrayList<OrderHistory> hist = new ArrayList<OrderHistory>();
        
        Connection c;
		try {
			c = DriverManager
			    .getConnection( url, username, password );
	
			Statement stmt = c.createStatement();
	        ResultSet rs = stmt.executeQuery("SELECT Entrees.Name, Orders.Timestamp  "+
	        		"FROM  `Orders` " + 
	        		"LEFT JOIN OrderToEntrees ON Orders.OrderID = OrderToEntrees.OrderID " +
	        		"LEFT JOIN Entrees ON OrderToEntrees.EntreeID = Entrees.EntreeID " +
	        		"WHERE Orders.CustomerID =2");
	        
        	while( rs.next() )
            {	            		
        		hist.add(new OrderHistory (rs.getString("Name"),  rs.getDate("Timestamp")) );
            }
        	
        	getServletContext().setAttribute( "history", hist );
        	request.getRequestDispatcher("/WEB-INF/server/HistoryView.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
